package cq.zhcqk.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import cq.zhcqk.R;
import cq.zhcqk.fragment._2_dj._3_DfFragment;
import cq.zhcqk.util.wxapi.Constants;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	private Context context;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wx_pay_result);
		context = this;
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
//		System.out.println("wx支付。。"+req);
	}

	@Override
	public void onResp(BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle(R.string.app_tip);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();
			switch (resp.errCode) {
				case 0:
					setPayResult(resp.errCode);
					break;
				case -1:
					setPayResult(resp.errCode);
					break;
				case -2:
					setPayResult(resp.errCode);
					break;
				default:

					break;
			}
		}
	}

	private void setPayResult(int payResultResult) {
		try {
			if(payResultResult != 0){
				Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(context, "支付成功", Toast.LENGTH_SHORT).show();
			}
			_3_DfFragment.payNot(payResultResult);
		}catch (Exception e){}finally {
			//结束掉自己
			finish();
		}
	}
}